﻿using System;
using System.Collections.Generic;
using MTC.Common.DTO;
using MTC.Web.Business.DTO;
using MTC.Business.Accident;
using Accident=MTC.Business.Accident.Accident;

namespace MTC.Web.Business
{
    public class Accidents
    {
        private List<AccidentEntity> _accidentEntityList = new List<AccidentEntity>();

        public List<AccidentEntity> GetAccidents()
        {
            List<MTC.Common.DTO.Accident> accidents;
            accidents = Accident.GetAccidents();
            if (accidents == null)
                return null;
            _accidentEntityList.Clear();
            foreach (MTC.Common.DTO.Accident accident in accidents)
            {
                AccidentEntity entity = new AccidentEntity();
                entity.AccidentDate = accident.AccidentDate;
                entity.AccidentId = accident.AccidentId;
                entity.Comments = accident.Comments;
                entity.Costs = accident.Costs;
                entity.Damages = accident.Damages;
                entity.DriverId = accident.DriverId;
                entity.Fault = accident.Fault;
                entity.Location = accident.Location;
                entity.RepairedLocation = accident.RepairedLocation;
                entity.TaxiId = accident.TaxiId;
                entity.Name = accident.Name;
                entity.PlateNumber = accident.PlateNumber;
                _accidentEntityList.Add(entity);
            }
            return _accidentEntityList;
        }

        public AccidentEntity GetAnAccident(int accidentid )
        {
            if (accidentid == 0)
                return null;
            AccidentEntity entity = null;
            MTC.Common.DTO.Accident accident = Accident.GetAnAccident(accidentid);
            if (accident != null)
            {
                entity = new AccidentEntity();
                entity.AccidentDate = accident.AccidentDate;
                entity.AccidentId = accident.AccidentId;
                entity.Comments = accident.Comments;
                entity.Costs = accident.Costs;
                entity.Damages = accident.Damages;
                entity.DriverId = accident.DriverId;
                entity.Fault = accident.Fault;
                entity.Location = accident.Location;
                entity.RepairedLocation = accident.RepairedLocation;
                entity.TaxiId = accident.TaxiId;
                entity.Name = accident.Name;
            }
            return entity;
        }

        public bool UpdateAnAccident(AccidentEntity entity)
        {
            if (entity==null)
            {
                return false;
            }

            MTC.Common.DTO.Accident accident = new Common.DTO.Accident();

            accident.AccidentDate = entity.AccidentDate;
            accident.AccidentId = entity.AccidentId;
            accident.Comments = entity.Comments;
            accident.Costs = entity.Costs;
            accident.Damages = entity.Damages;
            accident.DriverId = entity.DriverId;
            accident.Fault = entity.Fault;
            accident.Location = entity.Location;
            accident.RepairedLocation = entity.RepairedLocation;
            accident.TaxiId = entity.TaxiId;

            return Accident.UpdateAnAccident(accident);


        }

        public bool DeleteAccident(int AccidentId)
        {
            if (AccidentId == 0)
                return false;

            return Accident.DeleteAccident(AccidentId);
        }

        public int AddAccident(AccidentEntity entity)
        {
            if (entity == null)
            {
                return 0;
            }
            MTC.Common.DTO.Accident accident = new Common.DTO.Accident();

            accident.AccidentDate = entity.AccidentDate;
            accident.AccidentId = entity.AccidentId;
            accident.Comments = entity.Comments;
            accident.Costs = entity.Costs;
            accident.Damages = entity.Damages;
            accident.DriverId = entity.DriverId;
            accident.Fault = entity.Fault;
            accident.Location = entity.Location;
            accident.RepairedLocation = entity.RepairedLocation;
            accident.TaxiId = entity.TaxiId;

            return MTC.Business.Accident.Accident.AddAccident(accident);
        }
    }
}
